package com.mh.dao;

import com.mh.domain.AppRole;
import com.mh.domain.Customer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage AppRole entities.
 * 
 */
@Repository("AppRoleDAO")
@Transactional
public class AppRoleDAOImpl extends AbstractJpaDao<AppRole> implements
		AppRoleDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 * 
	 * @generated
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { AppRole.class }));
	/**
	 * EntityManager injected by Spring for persistence unit mhtest
	 *
	 * 
	 * @generated
	 */
	@PersistenceContext(unitName = "mhtest")
	private EntityManager entityManager;

	/**
	 * Instantiates a new AppRoleDAOImpl
	 *
	 * 
	 * @generated
	 */
	public AppRoleDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 * 
	 * @generated
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 * 
	 * @generated
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findAppRoleByDescription
	 *
	 */
	@Transactional
	public Set<AppRole> findAppRoleByDescription(String description) throws DataAccessException {

		return findAppRoleByDescription(description, -1, -1);
	}

	/**
	 * JPQL Query - findAppRoleByDescription
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppRole> findAppRoleByDescription(String description, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppRoleByDescription", startResult, maxRows, description);
		return new LinkedHashSet<AppRole>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppRoleByPrimaryKey
	 *
	 */
	@Transactional
	public AppRole findAppRoleByPrimaryKey(Integer id) throws DataAccessException {

		return findAppRoleByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findAppRoleByPrimaryKey
	 *
	 */

	@Transactional
	public AppRole findAppRoleByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAppRoleByPrimaryKey", startResult, maxRows, id);
			return (com.mh.domain.AppRole) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAppRoleByRoleNameContaining
	 *
	 */
	@Transactional
	public Set<AppRole> findAppRoleByRoleNameContaining(String roleName) throws DataAccessException {

		return findAppRoleByRoleNameContaining(roleName, -1, -1);
	}

	/**
	 * JPQL Query - findAppRoleByRoleNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppRole> findAppRoleByRoleNameContaining(String roleName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppRoleByRoleNameContaining", startResult, maxRows, roleName);
		return new LinkedHashSet<AppRole>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllAppRoles
	 *
	 */
	@Transactional
	public Set<AppRole> findAllAppRoles() throws DataAccessException {

		return findAllAppRoles(-1, -1);
	}

	/**
	 * JPQL Query - findAllAppRoles
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppRole> findAllAppRoles(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAppRoles", startResult, maxRows);
		return new LinkedHashSet<AppRole>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppRoleByDescriptionContaining
	 *
	 */
	@Transactional
	public Set<AppRole> findAppRoleByDescriptionContaining(String description) throws DataAccessException {

		return findAppRoleByDescriptionContaining(description, -1, -1);
	}

	/**
	 * JPQL Query - findAppRoleByDescriptionContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppRole> findAppRoleByDescriptionContaining(String description, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppRoleByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<AppRole>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppRoleById
	 *
	 */
	@Transactional
	public AppRole findAppRoleById(Integer id) throws DataAccessException {

		return findAppRoleById(id, -1, -1);
	}

	/**
	 * JPQL Query - findAppRoleById
	 *
	 */

	@Transactional
	public AppRole findAppRoleById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAppRoleById", startResult, maxRows, id);
			return (com.mh.domain.AppRole) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAppRoleByRoleName
	 *
	 */
	@Transactional
	public Set<AppRole> findAppRoleByRoleName(String roleName) throws DataAccessException {

		return findAppRoleByRoleName(roleName, -1, -1);
	}

	/**
	 * JPQL Query - findAppRoleByRoleName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppRole> findAppRoleByRoleName(String roleName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppRoleByRoleName", startResult, maxRows, roleName);
		return new LinkedHashSet<AppRole>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppRoleByApplicationContaining
	 *
	 */
	@Transactional
	public Set<AppRole> findAppRoleByApplicationContaining(String application) throws DataAccessException {

		return findAppRoleByApplicationContaining(application, -1, -1);
	}

	/**
	 * JPQL Query - findAppRoleByApplicationContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppRole> findAppRoleByApplicationContaining(String application, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppRoleByApplicationContaining", startResult, maxRows, application);
		return new LinkedHashSet<AppRole>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppRoleByApplication
	 *
	 */
	@Transactional
	public Set<AppRole> findAppRoleByApplication(String application) throws DataAccessException {

		return findAppRoleByApplication(application, -1, -1);
	}

	/**
	 * JPQL Query - findAppRoleByApplication
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppRole> findAppRoleByApplication(String application, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppRoleByApplication", startResult, maxRows, application);
		return new LinkedHashSet<AppRole>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(AppRole entity) {
		return true;
	}
	

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<AppRole> findAllAppRoles(AppRole approle, int startResult,
			int maxRows) {
		List lst = entityManager.createQuery("select myAppRole from AppRole myAppRole").getResultList();
		return (List<AppRole>)lst;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppRole> findAppRoles(AppRole appRole) throws DataAccessException {
		
		String queryFilter = "select appRole from AppRole appRole where 1=1 ";
		
		if (appRole.getId() != null && appRole.getId().toString() !=""){
			queryFilter += "and appRole.id = " + appRole.getId().toString() ;
		} else {
			if (appRole.getRoleName() != null && appRole.getRoleName() !=""){
				queryFilter += " and upper(appRole.roleName) like '%" + appRole.getRoleName().toUpperCase() + "%'";
			}
			if (appRole.getDescription() != null && appRole.getDescription() !=""){
				queryFilter += " and upper(appRole.description) like '%" + appRole.getDescription().toUpperCase()+ "%'";;
			}
			
			if (appRole.getApplication() != null && appRole.getApplication() !=""){
				queryFilter += " and upper(appRole.application) like '%" + appRole.getApplication().toUpperCase() + "%'"; ;
			}
			
		
		}
		return new LinkedHashSet<AppRole>(entityManager.createQuery(queryFilter).getResultList());
		
	}

	
}
